import random
from BinaryTree import TreeNode


def generate_tree(current_level, max_level, min_num, max_num):
    if current_level == max_level:
        return TreeNode(random.randint(min_num, max_num))
    node = TreeNode(random.randint(min_num, max_num))
    node.left = generate_tree(current_level + 1, max_level, min_num, max_num)
    node.right = generate_tree(current_level + 1, max_level, min_num, max_num)
    return node


def set_random_leaves(node, n, value):
    leaves = []

    def collect_leaves(_node):
        if _node is None:
            return
        if _node.left is None and _node.right is None:
            leaves.append(_node)
            return
        collect_leaves(_node.left)
        collect_leaves(_node.right)

    collect_leaves(node)

    if len(leaves) >= n:
        random_leaves = random.sample(leaves, n)
        for leaf in random_leaves:
            leaf.value = value
    return node


def generateMatrix(m: int = random.randint(5, 16), n: int = random.randint(5, 16)) -> list[list[int]]:
    return [generateRandomNumberList(m) for _ in range(n)]


def generateRandomNumberList(amount: int = random.randint(10, 20), min_num=0, max_num=9) -> list[int]:
    return [random.randint(min_num, max_num) for _ in range(amount)]
